package com.mingqijia.gassafety.webserver.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.db.entity.ResetDetail;
import com.mingqijia.gassafety.db.mapper.ResetDetailMapper;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.BeanConvertUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.ResetHandlerRequest;
import com.mingqijia.gassafety.webserver.request.ResetInsertRequest;
import com.mingqijia.gassafety.webserver.response.ResetHandlerResponse;
import com.mingqijia.gassafety.webserver.service.ResetDetailService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanghai
 * @since 2022-05-11
 */
@Service
public class ResetDetailServiceImpl extends ServiceImpl<ResetDetailMapper, ResetDetail> implements ResetDetailService {


    @Override
    public PageUtil<ResetHandlerResponse> detailList(ResetHandlerRequest req) {
        QueryWrapper<ResetDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ResetDetail::getLogId,req.getLogId())
                .eq(ResetDetail::getIsDeleted, Constants.IS_DELETED_FALSE)
                .orderByDesc(ResetDetail::getCreatedAt);
        Page<ResetDetail> page = PageHelper.startPage(req.getPage(), req.getPageSize());
        List<ResetDetail> resetDetailList = this.list(queryWrapper);
        List<ResetHandlerResponse> responses = BeanConvertUtils.convertListTo(resetDetailList, ResetHandlerResponse::new);
        PageUtil<ResetHandlerResponse> pageUtil = new PageUtil<>();
        pageUtil.setPage(req.getPage());
        pageUtil.setPageSize(req.getPageSize());
        pageUtil.setTotal(page.getTotal());
        pageUtil.setList(responses);
        return pageUtil;
    }

    @Override
    public void delete(String detailId) {
        ResetDetail resetDetail = new ResetDetail();
        resetDetail.setIsDeleted(true);
        resetDetail.setId(Long.valueOf(detailId));
        this.updateById(resetDetail);

    }

    @Override
    public void insert(ResetInsertRequest request) {
        String spId = ApplicationUtils.getWorkingSpId();
        String creator = ApplicationUtils.getWorkingUser().getUsername();
        ResetDetail detail = new ResetDetail();
        detail.setContent(request.getContent());
        detail.setLogId(request.getLogId());
        detail.setOperator(creator);
        detail.setOperatorName(ApplicationUtils.getWorkingUser().getName());
        detail.setSpId(spId);
        this.save(detail);
    }
}
